package com.psl.services.work;
import com.psl.Dao.Conn;
import com.psl.model.product;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductServices
{
  public List queryAllProduct()
  {
    Connection conn = new Conn().getConn();
    ResultSet rs = null;
    PreparedStatement pstmt = null;
    List product = new ArrayList();
    try
    {
      pstmt = conn.prepareStatement("SELECT p_no,p_name,p_size,p_r,p_n,p_uw,p_up,p_zx from product");
      rs = pstmt.executeQuery();
      while (rs.next())
      {
        product p = new product();
        p.setP_no(rs.getString(1));
        p.setP_name(rs.getString(2));
        p.setP_size(rs.getString(3));
        p.setP_r(rs.getString(4));
        p.setP_n(rs.getString(5));
        p.setP_uw(rs.getString(6));
        p.setP_up(rs.getString(7));
        p.setP_zx(rs.getString(8));
        product.add(p);
        HttpServletRequest request= ServletActionContext.getRequest();
        request.setAttribute("jist",product);
      }
      return product;
    }
    catch (SQLException e)
    {
      e.printStackTrace();
      return null;
    }
    finally
    {
      try
      {
        if (pstmt != null) {
          pstmt.close();
        }
        if (rs != null) {
          rs.close();
        }
        if (conn != null) {
          conn.close();
        }
      }
      catch (SQLException e)
      {
        e.printStackTrace();
      }
    }
  }
  
  public boolean insertProduct(String p_no, String p_name,String p_size, String p_r, String p_n, String p_uw, String p_up, String p_zx)
  {
    Connection conn = new Conn().getConn();
    PreparedStatement pstmt = null;
    try
    {
      pstmt = conn.prepareStatement("insert into product(p_no,p_name,p_size,p_r,p_n,p_uw,p_up,p_zx) VALUES (?,?,?,?,?,?,?,?)");
      pstmt.setString(1, p_no);
      pstmt.setString(2, p_name);
      pstmt.setString(3, p_size);
      pstmt.setString(4, p_r);
      pstmt.setString(5, p_n);
      pstmt.setString(6, p_uw);
      pstmt.setString(7, p_up);
      pstmt.setString(8, p_zx);
      pstmt.execute();
      return true;
    }
    catch (SQLException e)
    {
      e.printStackTrace();
    }
    finally
    {
      try
      {
        if (pstmt != null) {
          pstmt.close();
        }
        if (conn != null) {
          conn.close();
        }
      }
      catch (SQLException e)
      {
        e.printStackTrace();
      }
    }
    return true;
  }

  public boolean deleteProduct(String p_no)
  {
    Connection conn = new Conn().getConn();
    ResultSet rs = null;
    PreparedStatement pstmt = null;
    try
    {
      pstmt = conn.prepareStatement("delete FROM product WHERE p_no=?");
      pstmt.setString(1, p_no);
      rs = pstmt.executeQuery();
      if (rs.next()) {
        return true;
      }
      return true;
    }
    catch (SQLException e)
    {
      e.printStackTrace();
    }
    finally
    {
      try
      {
        if (pstmt != null) {
          pstmt.close();
        }
        if (rs != null) {
          rs.close();
        }
        if (conn != null) {
          conn.close();
        }
      }
      catch (SQLException e)
      {
        e.printStackTrace();
      }
    }
    return true;
  }
}
